import c4d
import os
import sys
import re

from c4d import gui, plugins, bitmaps, utils, documents

PLUGIN_ID = 1037228

INITIALIZATION_BTN = 1001
SAVE_PATH = 1002
AUTO_RENAME_BTN = 1003
ANIMATION_ATTACH_PATH = 1004
IS_LEGACY_ANIMATION = 1005
IS_LOOP_ANIMATION = 1006
ROOT_BONE_PATH = 1007
IS_ENABLE_ROOT_MOTION = 1008
IS_OUTPUT_MAT_AND_TEXTURE = 1009
PREFAB_ATTACH_PATH = 1010
ANIMATION_COMPRESSION_POS_ERROR = 1011
ANIMATION_COMPRESSION_SCALE_ERROR = 1012
ANIMATION_COMPRESSION_ROT_ERROR = 1013
DEFAULT_SHADER = 1014
RETENTION_TIME_LENGTH = 1015
ANIMATION_COMPRESSION_MODE = 1016
ANIMATION_COMPRESSION_MODE1 = 0
ANIMATION_COMPRESSION_MODE2 = 1
ANIMATION_COMPRESSION_MODE3 = 2
ANIMATION_COMPRESSION_MODE4 = 3
PURE_OUTPUT = 1017
ANIMATION_KEYFRAMED_ALL_OBJECTS = 1018
BUILD_AVATAR_INFO = 1019

class Entry(plugins.TagData):

    def Init(self, node):
    
        node[IS_OUTPUT_MAT_AND_TEXTURE] = True
        node[ANIMATION_COMPRESSION_MODE] = ANIMATION_COMPRESSION_MODE2
        node[ANIMATION_COMPRESSION_POS_ERROR] = 0.5
        node[ANIMATION_COMPRESSION_SCALE_ERROR] = 0.5
        node[ANIMATION_COMPRESSION_ROT_ERROR] = 0.5
        node[DEFAULT_SHADER] = "Standard"
        node[RETENTION_TIME_LENGTH] = True
    
        return True

    def Message(self, node, type, data):
    
        if type==c4d.MSG_DESCRIPTION_COMMAND:
            bt = data['id'][0].id 
            doc = c4d.documents.GetActiveDocument()
            
            if bt == INITIALIZATION_BTN:
                fileSourcePath = os.path.join(doc.GetDocumentPath(), doc.GetDocumentName())
                docDir = os.path.dirname(fileSourcePath)

                os.chdir(docDir)
                
                node.GetObject().SetName("UnityExportSetting")
                
                if not node[SAVE_PATH] == None and not node[SAVE_PATH] == "":
                    node[SAVE_PATH] = os.path.relpath(node[SAVE_PATH])
            
                if not node[ANIMATION_ATTACH_PATH] == None and not node[ANIMATION_ATTACH_PATH] == "":
                    node[ANIMATION_ATTACH_PATH] = os.path.relpath(node[ANIMATION_ATTACH_PATH])
            
                if not node[PREFAB_ATTACH_PATH] == None and not node[PREFAB_ATTACH_PATH] == "":
                    node[PREFAB_ATTACH_PATH] = os.path.relpath(node[PREFAB_ATTACH_PATH])
            
            elif bt == AUTO_RENAME_BTN:
                str = node[SAVE_PATH].replace("\\","/")
                
                if doc.GetDocumentName().find("@") > -1:
                    rawFileName = doc.GetDocumentName()
                    rawFileName = rawFileName.replace(".c4d", ".fbx")
                    
                    tmpInx = str.rfind("/")+1
                    templatePath = str[:tmpInx-1]
                    tmpInx = templatePath.rfind("/")+1
                    templatePath = templatePath[:tmpInx-1]
                    
                    tmpInx = rawFileName.rfind("@")+1
                    str = rawFileName[tmpInx:]
                    tmpInx = str.rfind(".")
                    parentPath = str[:tmpInx]
                    
                    finalPath = templatePath + "/" + parentPath + "/" + rawFileName

                    node[SAVE_PATH] = finalPath
                else:
                    rawFileName = doc.GetDocumentName()
                    rawFileName = rawFileName.replace(".c4d", ".fbx")
                    
                    tmpInx = str.rfind("/")+1
                    templatePath = str[:tmpInx-1]
                    tmpInx = templatePath.rfind("/")+1
                    templatePath = templatePath[:tmpInx-1]
                    
                    tmpInx = rawFileName.rfind(".")
                    fileName = rawFileName[:tmpInx]
                    
                    finalPath = templatePath + "/" + fileName + "/" + rawFileName

                    node[SAVE_PATH] = finalPath

                c4d.EventAdd()

        return True
        
if __name__ == "__main__":
    bmp = bitmaps.BaseBitmap()
    dir, file = os.path.split(__file__)
    fn = os.path.join(dir, "res", "tag.png")
    bmp.InitWith(fn)
    
    plugins.RegisterTagPlugin(id=PLUGIN_ID, str="Unity Base Setting Tag",
                              info=c4d.TAG_VISIBLE, g=Entry,
                              description="unitybasesetting", icon=bmp)